<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/19
 * Time: 14:50
 */

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class AdminController extends Controller
{

    //显示管理员列表
    public function index(){
        $users = DB::table('admin')->get();
        return $users;
    }
    //添加管理员
    public function store(Request $request){
        $username=$request->input('username');
        $pass=md5($request->input('password'));
        $group_id=$request->input('group_id');
        $res=DB::insert('insert into admin (username, password,group_id) values (?, ?,?)', [$username, $pass,$group_id]);
        $data=[];
        if($res){
            $data['msg']='添加成功';
            return  $data;
        }else{
            $data['msg']='添加失败';
            return  $data;
        }
    }
    //删除管理员
    public function destroy(Request $request){
        $id=$request->input('id');
        $deleted = DB::delete('delete from admin where id = ? ', [$id]);
        $data=[];
        if($deleted){
            $data['msg']='删除成功';
            $data['code']=1;
            return  $data;
        }else{
            $data['code']=0;
            $data['msg']='删除失败';
            return  $data;
        }

    }
    //修改管理员
    public function update(Request $request){
        $id=$request->input('id');
        $username=$request->input('username');
        $password=md5($request->input('password'));
        $group_id=$request->input('group_id');
        $res= DB::table('admin')->where(['id' => $id])->update(['username'=>$username,'password'=>$password,'group_id'=>$group_id]);
        $data=[];
        if($res){
            $data['msg']='修改成功';
            return  $data;
        }else{
            $data['msg']='修改失败';
            return  $data;
        }
    }

}